package com.zbowen.way.utils;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class ReadFileData {

    private static String[] fields = new String[]{"name", "duration", "distance", "restriction", "tolls", "lights", "strategy", "road", "record"};


    public static void main(String[] args) throws IOException, SQLException {
        FileReader reader = new FileReader("D:\\Users\\zbw\\Desktop\\way.txt");
        BufferedReader buf = new BufferedReader(reader);

        List<Map<String, Object>> list = new ArrayList<>();

        String line = null;
        int i = -1;
        int count = 0;

        Map<String, Object> map = new HashMap<>();

        while ((line = buf.readLine()) != null) {

            i++;
            if (i > 10){
                line = line.trim();
                count++;
                if (count == 3) {
                    i = 0;
                    count = 0;

                } else {
                    i = 3;

                }
            }

            if (i == 0) {
                line = line.substring(0, 8);
                map.put("record",  "2023-11-30 " + line);
            } else if (i == 1 || i == 2) {
                continue;
            } else if (i == 3) {
                // 路线1
                line = line.trim();
                map.put("name", line);
            } else if (i == 4) {
                // 耗时
                line = line.trim();
                map.put("duration", line);
            } else if (i == 5) {
                // 里程
                line = line.trim();
                map.put("distance", line);
            } else if (i == 6) {
                // 限行
                line = line.trim();
                map.put("restriction", line);
            } else if (i == 7) {
                // 收费
                line = line.trim();
                map.put("tolls", line);
            } else if (i == 8) {
                // 红绿灯
                line = line.trim();
                map.put("lights",line);
            } else if (i == 9) {
                // 策略
                line = line.trim();
                map.put("strategy",line);
            } else if (i == 10) {
                // 路线
                line = line.trim();
                map.put("road",line);

                Map<String, Object> temp = new HashMap<>();
                temp.putAll(map);
                list.add(temp);
            }
            System.out.println(line);
        }
        buf.close();


        String sql = "INSERT INTO zbw_way_info (name, duration, distance, restriction, tolls, lights, strategy, road, record) VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?)";
        Connection conn = DBUtils.getConnection();
        PreparedStatement pstmt = conn.prepareStatement(sql);

        for (Map<String, Object> d : list) {
            for (int j = 0; j < fields.length; j++) {
                pstmt.setObject(j+1, d.get(fields[j]));
            }
            pstmt.executeUpdate();
        }
        conn.close();
    }
}
